from django.shortcuts import render
from zi.models import *
from rest_framework.views import APIView
from rest_framework.response import Response
from .ser import *
# Create your views here.
#读取首页三级分类
class Home(APIView):
    def get(self,request):
        #读取一级
        cates = Category.objects.filter(parent_id__isnull=True).all()
        list=[]
        for i in cates:
            dict={}
            dict['id'] = i.id
            dict['name'] = i.name
            #读取二级
            two = i.subs.all()
            print(two)
            twolist=[]
            #遍历二级获取三级
            for j in two:
                twodict={}
                twodict['id'] = j.id
                twodict['name'] = j.name
                three = j.subs.all()
                ser = CateSer(three,many=True)
                twodict['subs'] = ser.data
                twolist.append(twodict)
            dict['subs'] = twolist
            list.append(dict)
        #查询一级分类，一级下面的二级，一级下面的三级，一级下面所有的东西
        categoods = []
        for j in cates:
            dict = {}
            dict['id']=j.id
            dict['name']=j.name
            dict['pic']=str(j.pic)
            two = j.subs.all()
            twoser = CateSer(two,many=True)
            dict['two'] = twoser.data
            #获取三级
            print(j.id)
            three = Category.objects.filter(top_parent=j.id,level=3).all()
            threeser = CateSer(three,many=True)
            dict['three'] = threeser.data
            #获取商品，排序，新品按时间倒序排，价格，q销量
            sorted = request.GET.get('sorted')
            topid = request.GET.get('topid')
            goods = Goods.objects.filter(category1_id=j.id,home_show=1).all()
            if sorted:
                if int(topid)==int(j.id):
                    goods = Goods.objects.filter(category1_id=j.id,home_show=1).order_by('-'+sorted).all()
            gser = CateSer(goods,many=True)
            dict['goods'] = gser.data
            categoods.append(dict)
        return Response({"code":200,'catelist':list,'categoods':categoods})
class CateM(APIView):
    def post(self,request):
        userid = request.data.get('user')
        goodsid = request.data.get('goods')
        number = int(request.data.get('number'))
        cart = Cart.objects.filter(user_id=userid,goods=goodsid).first()
        if cart:
            cart.number += number
            cart.save()
            return Response({'code':200})
        else:
            ser = CartSer(data=request.data)
            if ser.is_valid():
                ser.save()
                return Response({'code':200})
            else:
                print(ser.errors)
            return Response({'code':10010})
class GetCheckedCart(APIView):
    def get(self,request):
        userid = request.GET.get('userid')
        cart = Cart.objects.filter(user_id=userid,is_checked=True).all()
        ser = ShowCartSer(cart,many=True)
        total = 0
        tmoney = 0
        for i in ser.data:
            total += i['number'] * float(i['goods']['price'])
        return Response({'code':200,'list':ser.data,'total':total,'tmoney':tmoney})









